layui.use(['form','admin', 'ax', 'table', 'treetable'], function () {
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;
    var treetable = layui.treetable;
    var form = layui.form;

    var Current = {
        tableId: "table"
    };

    Current.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'name', sort: true, title: '应用名称'},
            {field: 'description', sort: true, title: '描述'},
            {field: 'appId', sort: true, title: '应用id'},
            {field: 'secretKey', sort: true, title: '加密密钥'},
            {field: 'status', sort: true, title: '状态',toolbar: '#statusTpl'},
            {field: 'createdTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 350}
        ]];
    };

    Current.onEdit = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑',
            area: ['60%','70%'],
            content: system.contextPath + '/admin/sysApplication/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && Current.initTable(Current.tableId)
            }
        });
    };

    Current.onAdd = function (data) {
        var param = data ? '?parentAppId=' + data.appId : '';
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加',
            area: ['60%','70%'],
            content: system.contextPath + '/admin/sysApplication/add' + param,
            end: function () {
                admin.getTempData('formOk') && Current.initTable(Current.tableId)
            }
        });
    };

    Current.setMenu = function (data) {
        top.layui.layer.open({
            type: 2,
            title: data.name + '应用的菜单设置',
            area: ['60%','70%'],
            content: system.contextPath + '/admin/menu/index?appId=' + data.appId
        });
    };

    Current.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(system.contextPath + "/admin/sysApplication/delete/id/" + data.id, function (res) {
               if(res.status){
                   system.success("删除成功!");
                   //重新加载表格
                   Current.initTable(Current.tableId)
               }else{
                   system.error("删除失败!" + res.msg + "!");
               }
            }, function (res) {
                system.error("删除失败!" + res.msg + "!");
            });
            ajax.start();
        };
        system.confirm("是否删除" + data.name + "?", operation);
    };

    Current.initTable = function (tableId, data) {
        return treetable.render({
            elem: '#' + tableId,
            url: system.contextPath + '/admin/sysApplication/search/all',
            where: data,
            page: false,
            height: "full-158",
            cellMinWidth: 100,
            cols: Current.initColumn(),
            treeColIndex: 2,
            treeSpid: "0",
            treeIdName: "id",
            treePidName: "pid",
            treeDefaultClose: true,
            treeLinkage: false
            ,isJump: true //是否允许点击节点时弹出新窗口跳转
            ,showLine: true  //是否开启连接线
            ,size: 'lg',
            done: function (res, curr, count) {
                count || this.elem.next('.layui-table-view').find('.layui-table-header').css('overflow', 'visible');
            $('.layui-table-body').find('div.layui-table-cell').find('select').each(function () {
                var $this = $(this);
                var val = $this.data('value');
                var text;
                $(this).children().each(function () {
                    if (val == this.value) {
                        text = this.text;
                        return;
                    }
                })
                //更新layui 选择器输入框的值
                $this.next().find('.layui-select-title').find('input').val(text);
                //更新layui 选择器的值 以及设置其样式
                $this.val(val).parents('div.layui-table-cell').css('overflow', 'visible');
            })
            form.render(null, 'table');
            }
        });
    };

    // 渲染表格
    var tableResult = Current.initTable(Current.tableId);

    $('#expandAll').click(function () {
        treetable.expandAll('#' + Current.tableId);
    });
    $('#foldAll').click(function () {
        treetable.foldAll('#' + Current.tableId);
    });
    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Current.onAdd();
    });

    // 工具条点击事件
    table.on('tool(' + Current.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Current.onEdit(data);
        } else if (layEvent === 'delete') {
            Current.onDelete(data);
        } else if (layEvent === 'add') {
            Current.onAdd(data);
        } else if (layEvent === 'setMenu') {
            Current.setMenu(data);
        }
    });
});
